﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL;
using Entity;
using BookStore.ShopCart;

namespace BookStore.Controllers
{
    public class ShopcartController : Controller
    {
        //
        // GET: /Shopcart/
        //购物车
        public ActionResult Index()
        {
                int a = Convert.ToInt32(Request.Cookies["User"].Values["UserId"]);
                
                string una = "";
                if (dl() == 0)
                {
                    una = "请登录！";
                }
                else {
                    una = Request.Cookies["User"].Values["UserName"];
                }
                ViewBag.uid = una;
                if (dl() == 0)
                {
                    return View(CarFactory.GetCar());
                }
                else
                {
                    List<CartItem> list = new List<CartItem>();
                    list = X_BShopcart.Get(dl());
                    Cart c = new Cart();
                    c.Items = list;
                    return View(c);
                }
        }
        //是否登陆
        public int dl()
        {
            int a = 0;
            if (Request.Cookies["User"] != null)
            {
                a = Convert.ToInt32(Request.Cookies["User"].Values["UserId"]);
                
                return a;
            }
            else
            {
               
                return a;
            }
           
        }
        public ActionResult AddCar(int x_bookid,int x_count)
        {
          
            List<BookInformatica> b = X_BShopcart.Getbookinfo(x_bookid);
            BookInformatica bo = new BookInformatica();
            bo.BookName = b[0].BookName;
            bo.BookId = b[0].BookId;
            bo.BookPrice = b[0].BookPrice;
            bo.BookPicture = b[0].BookPicture;
            Cart car = CarFactory.GetCar();
            car.Add(bo,x_count,dl());
            return Redirect("/Shopcart/Index");
        }
        public ActionResult Showcar()
        {
             return View();
             
        }
        //数量的修改
        [HttpPost]
        public ActionResult Updata(int id, int count)
        {
            Cart car = CarFactory.GetCar();
            car.UpdataNum(id, count,dl());
            string s = "成功！";
            return Json(s);
        }
        //删除
        public ActionResult Delbook(int bookid)
        {
            Cart car = CarFactory.GetCar();
            car.Delcar(bookid,dl());
            return Redirect("/Shopcart/Index");
        }


        [HttpPost ]
        public ActionResult xjhh(string cid)
        {
            string ass=cid;
            string[] str = ass.Split(',');
           
      return Json(ass);
        }
    }
}
